package com.zoe.app.controller;

import cn.hutool.json.JSONUtil;
import com.zoe.app.entity.User;
import com.zoe.app.validate.ValidateGroup;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.groups.Default;

/**
 * @author zoe.Zhang
 * @date 2022/06/20
 * @description
 */
@RestController
public class UserController {
    @PostMapping("/add")
    /**
     * 校验Insert分组，还有没有注明分组的字段
     * 如果不想这么写@Validated({Insert.class, Default.class}
     * 就需要把Insert继承Default接口
     */
    public String add(@Validated({ValidateGroup.Insert.class, Default.class}) @RequestBody User user) {
        System.out.println(user);
        return JSONUtil.toJsonStr(user);
    }

    /**
     * 只校验Update分组的字段
     *
     * @param user
     * @return
     */
    @PutMapping("/update")
    public String update(@Validated(ValidateGroup.Update.class) @RequestBody User user) {
        System.out.println(user);
        return JSONUtil.toJsonStr(user);
    }
}
